﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace THO7AlgoritmTimerApplication
{
    public static class Noise
    {
        public static Bitmap SaltNPepperNoise(Bitmap sourceImage, int percentage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            //copy to snell class for fast access
            snell retImg = new snell(returnImage);

            //lock images for use
            retImg.Lock();
            int number = ((retImg.Width * retImg.Height) / 100) * percentage;

            //set black and white color
            Random rnd = new Random();

            int x, y = 0;
            //random pixel times number
            for (int i = 0; i < number; i++)
            {
                x = rnd.Next(retImg.Width);
                y = rnd.Next(retImg.Height);
                int color = rnd.Next(1,3);
                if (color == 1)
                {
                    retImg.SetPixel(x, y, Color.White);
                }
                else
                {
                    retImg.SetPixel(x, y, Color.Black);
                }
            }
            //unlock and return image for showing
            retImg.Unlock(true);
            return returnImage;
        }

        public static Bitmap periodicNoise(Bitmap sourceImage, int rows, int thickness)
        {
            Bitmap returnImage = new Bitmap(sourceImage);

            //copy to snell class for fast access
            snell retImg = new snell(returnImage);

            //lock images for use
            retImg.Lock();

            //int x, y = 0;
            
            //  width/rows to define gap space
            int gap = ((retImg.Width + retImg.Height)/rows);

            int x, y, temp = 0;

            for (int i = 0; i < rows; i++)
            {
                y = 0;
                for (x = gap * i; x < retImg.Width; x++)
                {
                    //if i == 0, lijn dunner (/2)
                    if (i == 0)
                    {
                        for (int thick = 0; thick < thickness/2+1; thick++)
                        {
                            if (y < retImg.Height && x < retImg.Width - thick)
                            {
                                temp = x + thick;
                                retImg.SetPixel(temp, y, Color.White);
                            }
                        }
                    }
                    else
                    {
                        for (int thick = 0; thick < thickness; thick++)
                        {
                            if (y < retImg.Height && x < retImg.Width - thick)
                            {
                                temp = x + thick;
                                retImg.SetPixel(temp, y, Color.White);
                            }
                        }
                    }
                    y++;
                }
                x = 0;
                for (y = gap * i; y < retImg.Height; y++)
                {
                    // if i == 0 , lijn dunner (/2)
                    if (i == 0)
                    {
                        for (int thick = 0; thick < thickness/2+1; thick++)
                        {
                            if (x < retImg.Width && y < retImg.Height - thick)
                            {
                                temp = y + thick;
                                retImg.SetPixel(x, temp, Color.White);
                            }
                        }
                    }
                    else
                    {
                        for (int thick = 0; thick < thickness; thick++)
                        {
                            if (x < retImg.Width && y < retImg.Height - thick)
                            {
                                temp = y + thick;
                                retImg.SetPixel(x, temp, Color.White);
                            }
                        }
                    }
                    x++;
                }
            }

            //unlock and return image for showing
            retImg.Unlock(true);
            return returnImage;
        }
    }

    //gaussian noise 3 channel / white, speckle noise, 


}
